package com.yang.controller.wx;

import com.yang.bean.MarketUser;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.yvo.wx.SearchKeywordBean;
import com.yang.exception.WXException;
import com.yang.service.wx.WXSearchService;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @author: 杨庆
 * @since: 2022.9.9 16:11
 * @description:
 */

@RestController
@RequestMapping("wx/search/")
public class WXSearchController {

    @Autowired
    WXSearchService wxSearchService;

    @Autowired
    DefaultWebSessionManager defaultWebSessionManager;


    /**
    * @description: 根据关键词搜索
    [keyword]
    * @return: com.yang.bean.common.BaseRespVo
    * @author: 杨庆
    * @time: 2022.9.9 16:18
     */
    @RequestMapping("helper")
    public BaseRespVo helper(String keyword,HttpServletRequest request) {
        //需要获取登录的用户信息
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        String[] data = wxSearchService.selectByKeyword(keyword,marketUser);
        return  BaseRespVo.ok(data);
    }


    //TODO 历史搜索没有完成，shiro完成以后再写
    /**
    * @description: 默认 热门关键字的数字
    []
    * @return: com.yang.bean.common.BaseRespVo
    * @author: 杨庆
    * @time: 2022.9.9 16:37
     */
    @RequestMapping("index")
    public BaseRespVo index(HttpServletRequest request) {
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        SearchKeywordBean searchKeywordBean =  wxSearchService.selectIndex(marketUser);
        return  BaseRespVo.ok(searchKeywordBean);
    }

    /**
    * @description:n 删除用户的搜所历史 逻辑的删除 delete=0
    [request]
    * @return: com.yang.bean.common.BaseRespVo
    * @author: 杨庆
    * @time: 2022.9.10 10:07
     */
    @RequestMapping("clearhistory")
    public BaseRespVo clearhistory(HttpServletRequest request) {
        String header = request.getHeader("X-CskaoyanMarket-Token");
        //需要获取登录的用户信息
        MarketUser marketUser =null;
        try {
            Session session = defaultWebSessionManager.getSession(() -> header);
            marketUser = (MarketUser) session.getAttribute(header);
        } catch (Exception e) {
            throw new WXException("session丢失");
        }
        wxSearchService.clearhistory(marketUser);
        return  BaseRespVo.ok();
    }

}
